using System.Collections;
using UnityEngine;

public class PoolRulesStandard : PoolRulesBase
{
	protected float m_TimePerRound;

	protected bool m_Countdown = true;

	protected bool m_CueBallHitRail;

	protected bool m_UseGuidelines;

	protected CallPockets m_UseCallPocket;

	protected BallType m_WhiteHitBallType = BallType.NONE;

	protected int m_CurPlayerIndex;

	protected BasePlayer[] m_Players;

	private string m_AnalyticPlayName = "Standand";

	protected int m_OpponentPlayerIndex => MathTools.Roll(0, m_Players.Length, m_CurPlayerIndex + 1);

	public BasePlayer CurrentPlayer => m_Players[m_CurPlayerIndex];

	public BasePlayer OpponentPlayer => m_Players[m_OpponentPlayerIndex];

	protected override void Start()
	{
		m_CurPlayerIndex = Random.Range(0, 2);
		m_TimePerRound = 30f;
		m_Time = m_TimePerRound;
		bool flag = false;
		for (int i = 0; i < m_Players.Length; i++)
		{
			if (m_Players[i] is AIPlayer)
			{
				flag = true;
				m_Players[i].name = m_Players[i].playerName.text = "玩家 " + Random.Range(1000, 100000);
			}
			else
			{
				m_Players[i].name = m_Players[i].playerName.text = "你";
			}
		}
		if (flag)
		{
			m_AnalyticPlayName = "Stardand-AI";
		}
		BaseUIController.text.Show(string.Format(HOLocalizationConfiguration.GetValue(401), "' " + CurrentPlayer.name + " '"));
		base.Start();
	}

	private bool AnyBallWithTypeEnterPocket(BallType type)
	{
		if (type == BallType.NONE)
		{
			if (m_PottedBallListThisRound.Count > 0)
			{
				return true;
			}
			return false;
		}
		int i = 0;
		for (int count = m_PottedBallListThisRound.Count; i < count; i++)
		{
			if (m_PottedBallListThisRound[i].ballType == type)
			{
				return true;
			}
		}
		return false;
	}

	protected override void CustomUpdate()
	{
		if (m_Countdown)
		{
			m_Time -= Time.deltaTime;
			CurrentPlayer.Countdown(m_Time / m_TimePerRound);
			if (m_Time <= 0f)
			{
				m_Countdown = false;
				m_TimeOut = true;
				StartCoroutine(CheckResultAndChangeTurn(0f));
			}
		}
		if (UnityEngine.Input.GetKeyDown(KeyCode.T))
		{
			BaseUIController.MSettlement.StandardGameOver(CurrentPlayer, OpponentPlayer);
		}
	}

	public override void SetPlayers(params IPlayer[] players)
	{
		int num = players.Length;
		m_Players = new BasePlayer[num];
		for (int i = 0; i < num; i++)
		{
			m_Players[i] = (BasePlayer)players[i];
		}
	}

	protected override void TurnBegin()
	{
		base.TurnBegin();
		if (m_HandleWhiteball)
		{
			base.State = GlobalState.DRAG_WHITEBALL;
		}
		m_Time = m_TimePerRound;
		m_Countdown = true;
		m_CueBallHitRail = false;
		m_WhiteHitBallType = BallType.NONE;
		m_HittedRailBallsThisRound.Clear();
		int i = 0;
		for (int num = m_Players.Length; i < num; i++)
		{
			m_Players[i].UpdateBallIcon();
		}
		CurrentPlayer.Begin();
		if (!(CurrentPlayer is AIPlayer))
		{
		}
	}

	protected override IEnumerator CheckResultAndChangeTurn(float time)
	{
		yield return new WaitForSeconds(time);
		if (CheckGameOver())
		{
			GameManager.Rules.State = GlobalState.GAMEOVER;
			if (PoolRulesBase.onGameOver != null)
			{
				PoolRulesBase.onGameOver(CurrentPlayer);
			}
			BaseUIController.MSettlement.StandardGameOver(CurrentPlayer, OpponentPlayer);
			yield break;
		}
		m_HandleWhiteball = HandleFouls();
		if (m_HandleWhiteball || !AnyBallWithTypeEnterPocket(CurrentPlayer.TargetBallType))
		{
			CurrentPlayer.combo = 0;
			CurrentPlayer.End();
			m_CurPlayerIndex = MathTools.Roll(0, m_Players.Length, m_CurPlayerIndex + 1);
			Invoke("showBaiQiu", m_HandleWhiteball ? 3 : 0);
		}
		int i = 0;
		for (int count = m_PottedBallListThisRound.Count; i < count; i++)
		{
			PoolBall poolBall = m_PottedBallListThisRound[i];
			m_PottedBallList.Add(poolBall.GetBallID(), poolBall);
		}
		m_PottedBallListThisRound.Clear();
		TurnBegin();
	}

	private void showBaiQiu()
	{
		BaseUIController.text.Show(string.Format(HOLocalizationConfiguration.GetValue(402), "' " + CurrentPlayer.name + " '"));
	}

	public override void BallHitRail(PoolBall poolBall)
	{
		if (m_WhiteHitBall)
		{
			base.BallHitRail(poolBall);
		}
	}

	public override bool CheckGameOver()
	{
		if (Pools.Balls[8].BallState != PoolBall.State.POTTED)
		{
			return false;
		}
		CurrentPlayer.UpdateBallIcon();
		if (base.firstRound)
		{
			return true;
		}
		if (CurrentPlayer.BallsList.Count > 0 || Pools.CueBall.BallState == PoolBall.State.POTTED)
		{
			m_CurPlayerIndex = m_OpponentPlayerIndex;
			return true;
		}
		return true;
	}

	protected override bool HandleFouls()
	{
		if (m_WhiteBallPotted)
		{
			return BaseUIController.text.Show(string.Format(HOLocalizationConfiguration.GetValue(409), "' " + CurrentPlayer.name + " '"));
		}
		if (m_TimeOut)
		{
			return BaseUIController.text.Show(HOLocalizationConfiguration.GetValue(412));
		}
		if (base.firstRound)
		{
			if (m_HittedRailBallsThisRound.Count < 4 && m_PottedBallListThisRound.Count == 0)
			{
				return BaseUIController.text.Show(HOLocalizationConfiguration.GetValue(413));
			}
		}
		else
		{
			if (!m_WhiteHitBall)
			{
				return BaseUIController.text.Show(HOLocalizationConfiguration.GetValue(403));
			}
			if ((m_WhiteHitBallType != CurrentPlayer.TargetBallType && CurrentPlayer.TargetBallType != BallType.NONE) || (CurrentPlayer.TargetBallType == BallType.NONE && m_WhiteHitBallType == BallType.BLACK))
			{
				return BaseUIController.text.Show(string.Format(HOLocalizationConfiguration.GetValue(408), "' " + CurrentPlayer.name + " '"));
			}
			if (m_WhiteHitBallType != BallType.NONE && m_HittedRailBallsThisRound.Count == 0 && m_PottedBallListThisRound.Count == 0)
			{
				return BaseUIController.text.Show(HOLocalizationConfiguration.GetValue(407));
			}
		}
		return false;
	}

	protected override void CallPocket()
	{
	}

	public override void OnBallFired()
	{
		base.OnBallFired();
		m_Countdown = false;
	}

	public override void WhiteBallHitBall(PoolBall ball)
	{
		if (!m_WhiteHitBall)
		{
			m_WhiteHitBallType = ball.ballType;
		}
		base.WhiteBallHitBall(ball);
	}

	public override void PotBall(PoolBall ball, PocketIndexes pocket)
	{
		base.PotBall(ball, pocket);
		if (CurrentPlayer.TargetBallType == BallType.NONE && ball.ballType != 0 && ball.ballType != BallType.BLACK && !base.firstRound)
		{
			CurrentPlayer.TargetBallType = ball.ballType;
			m_WhiteHitBallType = ball.ballType;
			OpponentPlayer.TargetBallType = ((ball.ballType != BallType.SOLID) ? BallType.SOLID : BallType.STRIPE);
		}
		if (ball.ballType != 0)
		{
			CurrentPlayer.combo++;
		}
	}

	public override void CueBallHitRail()
	{
		if (m_WhiteHitBall)
		{
			m_CueBallHitRail = true;
		}
	}
}
